<?php

namespace addons\yunfood\controller;

use addons\yunfood\library\Super;
use addons\yunfood\model\ShopDesk;
use addons\yunfood\model\Slide;
use addons\yunfood\model\Food;
use addons\yunfood\model\Shop;

/**
 * 页面接口
 */
class Page extends BaseApi
{
    /**
     * 获取首页信息
     */
    public function getHomeData()
    {
        $data = $this->request->post();

        // 门店数据
        $shopData = $this->validShop(false);
        $shopData = Super::arrayGetSet($shopData, [
            'id',
            'name',
            'cover_url',
            'start_time' => 'start_time_text',
            'end_time'   => 'end_time_text',
            'address',
            'contact',
            'state'
        ]);

        // 桌号数据
        $deskData = null;
        if ($deskId = $data['desk_id'] ?? null) {
            if ($deskData = ShopDesk::get(['id' => $deskId, 'shop_id' => $this->shopId])) {
                $deskData = Super::arrayGetSet($deskData, [
                    'id',
                    'number',
                ]);
            }
        }

        // 轮播列表
        $slideList = Slide::where(['position' => Slide::POSITION_HOME])->show()
            ->where('find_in_set(:sid, `shop_ids`) or shop_ids=\'\'', ['sid' => $this->shopId])
            ->order(['weigh' => 'ASC', 'id' => 'DESC'])->select();
        $slideList = Super::arrayGetSet($slideList, [
            'id',
            'title',
            'image_url',
            'redirect_type',
            'redirect_data'
        ], true);

        // 菜品上新
        $newList = Food::with(['categoryData'])->show()
            ->where('find_in_set(:sid, `shop_ids`) or shop_ids=\'\'', ['sid' => $this->shopId])
            ->order(['weight' => 'DESC', 'id' => 'DESC'])->limit(5)->select();
        $newList = Super::arrayGetSet($newList, [
            'id',
            'name',
            'cover_url',
            'category_id',
            'category_name' => function ($item) {
                return $item->categoryData->name ?? '--';
            },
            'multi_spec',
            'price',
            'market_price',
            'count_left',
            'count_sale',
            'count_like',
        ], true);

        // 推荐菜品
        $foodList = Food::with(['categoryData'])->show()
            ->where('find_in_set(:sid, `shop_ids`) or shop_ids=\'\'', ['sid' => $this->shopId])
            ->order(['weight' => 'DESC', 'id' => 'DESC'])->limit(5)->select();
        $foodList = Super::arrayGetSet($foodList, [
            'id',
            'name',
            'cover_url',
            'category_id',
            'category_name' => function ($item) {
                return $item->categoryData->name ?? '--';
            },
            'multi_spec',
            'price',
            'market_price',
            'count_left',
            'count_sale',
            'count_like',
        ], true);

        $result = [
            'shop_data'  => $shopData,
            'desk_data'  => $deskData,
            'slide_list' => $slideList,
            'new_list'   => $newList,
            'food_list'  => $foodList,
        ];

        $this->success(__('success'), $result);
    }

    /**
     * 获取点餐信息
     */
    public function getOrderData()
    {
        // 轮播列表
        $slideList = Slide::where(['position' => Slide::POSITION_ORDER])->show()
            ->where('find_in_set(:sid, `shop_ids`) or shop_ids=\'\'', ['sid' => $this->shopId])
            ->order(['weigh' => 'ASC', 'id' => 'DESC'])->select();
        $slideList = Super::arrayGetSet($slideList, [
            'id',
            'title',
            'image_url',
            'redirect_type',
            'redirect_data'
        ], true);

        $result = [
            'slide_list' => $slideList,
        ];

        $this->success(__('success'), $result);
    }
}
